package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class WineTypeController {
    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 302-Found (e.g. already found in DB)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    
    

    // REST operations
    // GET
    def show = {
//        println 'WineTypeController >> Show >> START'
        
        WineTypeManagement wtman = new WineTypeManagement()
        
//        if (params.id && WineType.exists(params.id)) {    
//            def wt = wtman.getWineTypeByID(params.id)
//            render wt as JSON
//            sendResponse(200)
//            
//            println 'Show wine type by ID >> ' + wt.id + " " + wt
//        }
        if (params.id) {
            if (params.id.isInteger() == true) {
                if (WineType.exists(params.id)) {
                    def wt = wtman.getWineTypeByID(params.id)
                    render wt as JSON
                    sendResponse(200)

//                    println 'Show wine type by ID >> ' + wt.id + " " + wt
                }
                else {
                    sendResponse(404)
                }
            }      
            else {
                def wt = wtman.getWineTypeByWineTypeName(params.id)
                if (wt == null) {
                    sendResponse(404)
                } 
                else {
                    render wt as JSON
                    sendResponse(200)
                }
            }
        }
        else {
            def allWT = wtman.listWineTypes()
            if (allWT.length() > 0) {
                render allWT as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }  
            
//            println 'Listing all wine cats >> ' + allWT
        }
        
//        println 'WineTypeController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'WineTypeController >> Update >> START'
        
//        JSONObject o
//        def wt
//        def id
//        def wineType
//        def wineDesc
        
        WineTypeManagement wtman = new WineTypeManagement()
        
        
//        if (params.wineType && params.wineDesc) {
//            println 'WineTypeController >> Update >> NO JSON'
//            wt = wtman.getWineTypeByID(params.id)      
//            wineType = params.wineType
//            wineDesc = params.wineDesc
//            
////            wt.id = id
//            wt.wineTypeName = wineType
//            wt.description = wineDesc
//        }
//        else {
//            println 'WineTypeController >> Update >> JSON'
//            o = request.JSON
//            
//            wt = wtman.getWineTypeByID(o.get("id"))
//            if (o.has("wineTypeName")) { wt.wineTypeName = o.get("wineTypeName"); println wt.wineTypeName }
//            if (o.has("description")) { wt.description = o.get("description"); println wt.description }
//        }
//        
//        def result = wtman.updateWineType(wt)
//        if (result == true) {
//            println 'WineTypeController >> Update >> ' + result
////            render response as JSON
//        }
//        else {
//            println 'WineTypeController >> Update >> ' + result
//        }
           
        JSONObject o = request.JSON
            
        def wt = wtman.getWineTypeByID(o.get("id"))
        if (o.has("wineTypeName")) { wt.wineTypeName = o.get("wineTypeName"); println wt.wineTypeName }
        if (o.has("description")) { wt.description = o.get("description"); println wt.description }
        
        def result = wtman.updateWineType(wt)
        if (result == true) {
            println 'WineTypeController >> Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'WineTypeController >> Update >> ' + result
            sendResponse(400)
        }
        

        println 'WineTypeController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'WineTypeController >> delete >> START'
        
        WineTypeManagement wtman = new WineTypeManagement()
        
        if (params.id && WineType.exists(params.id)) {    
            def result = wtman.deleteWineType(id)
            
            if (result == true) {
                println 'WineTypeController >> delete >> ' + result
                sendResponse(200)
            }
            else {
                println 'WineTypeController >> delete >> ' + result
                sendResponse(400)
            }
        }
        
        println 'WineTypeController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'WineTypeController >> Save >> START'
        
        JSONObject o = request.JSON
        
        println o
        
        WineTypeManagement wtman = new WineTypeManagement()
//        def wineType = params['wineType']
//        println wineType

        if (isUniqueWineTypeName(o.get("wineTypeName")) == true) {
            def result = wtman.addWineType(o)
            if (result == true) {
                println 'WineTypeController >> Save >> ' + result
                sendResponse(201)
            }
            else {
                println 'WineTypeController >> Save >> ' + result
                sendResponse(400)
            }
        }
        else {
            sendResponse(406)
        }
        
        println 'WineTypeController >> Save >> END'
        redirect(uri:"/wineTypeCreate")
    }
    
    
    // supporting operations
    def isUniqueWineTypeName(wineTypeName) {
        
        def status = false
        
        WineTypeManagement wtman = new WineTypeManagement()
        WineType wt = null
        
        wt = wtman.getWineTypeForDbByWineTypeName(wineTypeName)
        
        if (wt == null) {
            status = true 
        }
        
        return status
    }
   
}